home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / ZMODEM / RSCTRL.C < prev    next >
C/C++ Source or Header  |  1993-12-16  |  9KB  |  422 lines

  1. /*************************************************************************
  2. *    "rsctrl.c" : RS-232C ÉºîΣ
  3. *************************************************************************/
  4.  
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <stdarg.h>
  8. #include    <string.h>
  9. #include    <time.h>
  10. #include    <dos.h>
  11. #include    <msdos.cf>
  12.  
  13. #include    <splib.h>
  14. #include    <setupif.h>
  15. #define        _RSCTRL_MAIN
  16. #include    "rsctrl.h"
  17.  
  18. #ifdef    __HIGHC__
  19. #    pragma    On(Align_labels);
  20. #endif
  21.  
  22. #ifndef    MALLOC
  23. #    define    MALLOC    malloc
  24. #endif
  25. #ifndef    CALLOC
  26. #    define    CALLOC    calloc
  27. #endif
  28. #ifndef    FREE
  29. #    define    FREE    free
  30. #endif
  31.  
  32. int        RS_reopen( int port, int md, int bp )
  33. {
  34.     int         er;
  35.     RSB_T    *rsb = RsCtrl[port].rsb;
  36.  
  37.     if ( rsb == NULL )
  38.         return (ERR);
  39.  
  40.     RSB_DTR(port,1);        /*    DTRÉMìåé╠ò█Ä¥    */
  41.     RSB_CLOSE(port);
  42.  
  43. #ifdef    _RS_FMC
  44.     rsb->mode        = md;                /*    Æ╩ÉMâéü[âh                */
  45.     rsb->baud        = bp;                /*    â{ü[âîü[âg                */
  46. //    rsb->rbuf        = 0;
  47. //    rsb->rbuf_seg    = 0;
  48.     rsb->stime       = 500;                /*    æùÉMâ^âCâÇâAâEâg        */
  49.     rsb->rtime       = 500;                /*    Ä≤ÉMâ^âCâÇâAâEâg        */
  50.     rsb->rinfbuf     = 0;                /*    Ä≤ÉMÆ╩ÆmâAâhâîâX        */
  51.     rsb->rinfbuf_seg = 0;
  52.     rsb->extmode     = 0;                /*    ègÆúâéü[âh                */
  53. //    rsb->xon         = 0;                /*    XONâRü[âh(0x11)            */
  54. //    rsb->xoff        = 0;                /*    XOFFâRü[âh(0x13)        */
  55. //    rsb->obuf        = 0;
  56. //    rsb->obuf_seg    = 0;
  57. #else
  58. #ifdef    _RS_N10
  59.     rsb->mode    = md;                /*    Æ╩ÉMâéü[âh                */
  60.     rsb->baud    = bp;                /*    â{ü[âîü[âg                */
  61.     rsb->stime   = 0;                /*    æùÉMâ^âCâÇâAâEâg        */
  62.     rsb->rtime   = 0;                /*    Ä≤ÉMâ^âCâÇâAâEâg        */
  63.     rsb->rinfbuf = 0;                /*    Ä≤ÉMÆ╩ÆmâAâhâîâX        */
  64.     rsb->extmode = 0;                /*    ègÆúâéü[âh                */
  65.     rsb->xon     = 0;                /*    XONâRü[âh(0x11)            */
  66.     rsb->xoff    = 0;                /*    XOFFâRü[âh(0x13)        */
  67. #endif
  68. #ifdef    _RS_N11
  69.     rsb->mode    = md & (~RSMD_EXINT);    /*    Æ╩ÉMâéü[âh                */
  70.     rsb->baud    = bp;                    /*    â{ü[âîü[âg                */
  71.     rsb->stime   = 0;                    /*    æùÉMâ^âCâÇâAâEâg        */
  72.     rsb->rtime   = 0;                    /*    Ä≤ÉMâ^âCâÇâAâEâg        */
  73.     rsb->rinfofs = 0;                    /*    Ä≤ÉMÆ╩ÆmâAâhâîâX        */
  74.     rsb->rinfsel = 0;                    /*    Ä≤ÉMÆ╩ÆmâZâîâNâ^        */
  75.     rsb->extmode = 0;                    /*    ègÆúâéü[âh                */
  76.     rsb->xon     = 0;                    /*    XONâRü[âh(0x11)            */
  77.     rsb->xoff    = 0;                    /*    XOFFâRü[âh(0x13)        */
  78.     rsb->sendofs = 0;                    /*    æùÉMâoâbâtâ@âAâhâîâX    */
  79.     rsb->sendsel = 0;                    /*    æùÉMâoâbâtâ@âZâîâNâ^    */
  80. #endif
  81. #endif
  82.     RsCtrl[port].overFlag = 0;
  83.  
  84. #ifdef    _RS_FMC
  85.     if ( (er = RSB_setpara(port, rsb)) == 0 )
  86. #else
  87.     if ( (er = RSB_Setpara(port, (char *)rsb)) == 0 )
  88. #endif
  89.     {
  90.         er = RSB_OPEN(port);
  91.         RSB_INITBUF( port );
  92.     }
  93.  
  94.     return (er);
  95. }
  96.  
  97. void    RS_close(int port)
  98. {
  99.     --RsCtrl[port].use;
  100.     if ( RsCtrl[port].use <= 0 )
  101.     {
  102.         RsCtrl[port].use = 0;
  103.         RSB_CLOSE( port );
  104.     }
  105. }
  106.  
  107. static    int        RsbLimMax = (3*RS_BUFSIZ/4) & 0xFFFFFF00;
  108. static    int        RsbLimMin = (1*RS_BUFSIZ/4) & 0xFFFFFF00;
  109.  
  110. RSB_T    *RS_open( int port )
  111. {
  112.     RSB_T                *rsb;
  113.  
  114.     RS_init();
  115.  
  116.     if ( (rsb = RsCtrl[port].rsb) == NULL )
  117.     {
  118.         if ( (rsb = CALLOC(sizeof(RSB_T),1)) == NULL )
  119.             return (NULL);
  120.         RsCtrl[port].rsb = rsb;
  121.     }
  122. #ifdef    _RS_FMC
  123. #if    0
  124.     {
  125.         RSB_PARA    rsbPara;
  126.         _far void    *ptr;
  127.         size_t        bufsiz;
  128.  
  129.         RSB_rdrpara(port, &rsbPara );
  130.         _FP_SEG(ptr) = rsbPara.rbuf_seg;
  131.         _FP_OFF(ptr) = rsbPara.rbuf;
  132.         bufsiz = *(_far unsigned int *)(ptr);
  133.         RsbLimMax = 3 * bufsiz / 4;
  134.         RsbLimMin = 1 * bufsiz / 4;
  135.     }
  136. #else
  137.         RsbLimMax = 3 * 1024 / 4;
  138.         RsbLimMin = 1 * 1024 / 4;
  139. #endif
  140.     if ( RsCtrl[port].use == 0 )
  141.     {
  142.         RSB_rdrpara(port, rsb );
  143.         RSB_setpara(port, rsb);
  144.         RSB_open(port);
  145.         RsCtrl[port].overFlag = 0;
  146.     }
  147. #else
  148.     /* NATIVE RS */
  149.     {
  150.         RSBUF_T                *rsBuf;
  151.         if ( (rsBuf = RsCtrl[port].rsBuf) == NULL )
  152.         {
  153.             if ( (rsBuf = CALLOC(sizeof(RSBUF_T),1)) == NULL )
  154.                 return (NULL);
  155.             rsBuf->len = RS_BUFSIZ;
  156.             RsCtrl[port].rsBuf = rsBuf;
  157.         }
  158.  
  159.         if ( RsCtrl[port].use == 0 )
  160.         {
  161.             static RSYSINF_T    *rsys = NULL;
  162.  
  163.             if ( rsys == NULL )
  164.             {
  165.                 if ( (rsys = MALLOC(sizeof(RSYSINF_T))) == NULL )
  166.                     return (NULL);
  167.                 ESR_setupInfo(rsys);
  168.             }
  169.             rsb->baud = rsys->rs232c[port].baud;
  170.             rsb->mode = rsys->rs232c[port].mode; /* & (~RSMD_EXINT); */
  171.             rsb->rbuf = (char *)rsBuf;        /*    Ä≤ÉMâoâbâtâ@âAâhâîâX    */
  172.             rsb->sel  = getds();            /*    âZâîâNâ^ü[Æl            */
  173. #ifdef        _RS_N11
  174.             RSB_Setpara(port, (char *)rsb);
  175. #endif
  176.             RSB_OPEN(port);
  177.             RsCtrl[port].overFlag = 0;
  178.         }
  179.     }
  180. #endif
  181.     ++RsCtrl[port].use;
  182.  
  183.     return (rsb);
  184. }
  185.  
  186. int        RS_chk(int port)
  187. {
  188.     int        len;
  189.  
  190.     if ( RsCtrl[port].use == 0 )
  191.         return (ERR);
  192.  
  193.     RSB_READ(port,&len);
  194.     if ( RsCtrl[port].overFlag == FALSE )
  195.     {
  196.         if ( len > RsbLimMax )
  197.         {    RsCtrl[port].overFlag = TRUE;
  198.             RSB_CTRL(port,0x02);
  199.         }
  200.     } else
  201.     {
  202.         if ( len < RsbLimMin )
  203.         {    RsCtrl[port].overFlag = FALSE;
  204.             RSB_CTRL(port,0x22);
  205.         }
  206.     }
  207.     return (len);
  208. }
  209.  
  210. int        RS_getc(int port)
  211. {
  212.     UINT    i;
  213.     UINT    st;
  214.     int        ch;
  215.  
  216.     if ( RsCtrl[port].use == 0 )
  217.         return (ERR);
  218.  
  219. RETRY:
  220.     i = 0;
  221.     for(;;)
  222.     {
  223.         if ( RSB_RECEIVE(port,&ch,&st) == 0 )
  224.             break;
  225.         if ( ++i > RS_TIMEOUT_LIMIT )
  226.         {    ch = 0;
  227.             break;
  228.         }
  229.     }
  230.     ch &= 0xFF;
  231. #ifdef    _BPLUS_
  232.     if ( ch == 0x05 && (RsCtrl[port].attr & RSATT_BP) )
  233.     {
  234.         BP_Term_ENQ(port);
  235.         goto RETRY;
  236.     } else if ( ch == 0x10 && (RsCtrl[port].attr & RSATT_BP) )
  237.     {
  238.         BP_DLE_Seen(port);
  239.         goto RETRY;
  240.     }
  241. #endif
  242.     return    (ch);
  243. }
  244.  
  245. void    RS_putc(int port, int ch)
  246. {
  247.     if ( RsCtrl[port].use == 0 )
  248.         return;
  249.  
  250. #ifdef    _RS_FMC
  251.     {
  252.         UINT            st;
  253.  
  254.         RSB_send(port,ch,&st);
  255.     }
  256. #else
  257.     {
  258.         unsigned int    status, serial;
  259.  
  260.         if ( RSB_Status(port,&status,&serial) == 0 )
  261.         {
  262.             clock_t        clk;
  263.  
  264.             if ( !(serial & 2) )
  265.             {    /* CTS ÉMìåé¬OFFüiæùÉMé┼é½é╚éóüj    */
  266.                 int        i;
  267.  
  268.                 for ( i = 0; i < 5; ++i )
  269.                 {
  270.                     clk = H_CLOCK2(0) + 5 * CLOCKS_PER_SEC / 100;    /* 0.05òb    */
  271.                     while ( clk > H_CLOCK2(clk) )
  272.                         ;
  273.                     if ( RSB_Status(port,&status,&serial) )
  274.                         return;
  275.                     if ( serial & 2 )
  276.                         break;
  277.                 }
  278.             }
  279.  
  280.             clk = H_CLOCK2(0) + 50 * CLOCKS_PER_SEC / 100;    /* 0.5òb    */
  281.             do
  282.             {
  283.                 int        ret;
  284.  
  285.                 if ( (ret = RSB_Send(port,ch,&status)) == 0 )
  286.                     break;
  287.                 switch ( ret )
  288.                 {
  289.                     case 2:    /* ö═ê═ê╚èOé╠â|ü[âg                */
  290.                     case 3:    /* ègÆúâJü[âhé¬É┌æ▒é│éΩé─éóé╚éó    */
  291.                     case 4:    /* ë±Éⁿé¬âIü[âvâôé│éΩé─éóé╚éó    */
  292.                         return;
  293.                     case 8:    /* â^âCâÇâAâEâg                    */
  294.                     default:
  295.                         break;
  296.                 }
  297.             } while ( clk > H_CLOCK2(clk) );
  298.         }
  299.     }
  300. #endif
  301. }
  302.  
  303. void    RS_puts( int port, CONST char *s )
  304. {
  305.     while ( *s )
  306.     {
  307.         RS_putc( port, (*s) & 0xFF );
  308.         ++s;
  309.     }
  310. }
  311.  
  312. void    RS_printf( int port, CONST char *form, ... )
  313. {
  314.     va_list        arg;
  315.     char        tmp[BUFSIZ];
  316.  
  317.     va_start( arg, form );
  318.     vsprintf( tmp, form, arg );
  319.     va_end(arg);
  320.     RS_puts( port, tmp );
  321. }
  322.  
  323. void    RS_breakOut( int port, int tm )
  324. {
  325.     if ( RsCtrl[port].use == 0 )
  326.         return;
  327.     RSB_BREAK(port, tm );
  328. }
  329.  
  330. static    char    rsInit = FALSE;
  331.  
  332. /*************************************************************************
  333. *    êΩÄ₧ÆΓÄ~Åêù¥üiÄqâvâìâZâXî─é╤Åoé╡é╠é╜é▀üj
  334. *-------------------------------------------------------------------------
  335. *    üª    é▒é╠è╓Éöé≡Ägé┴é╜îπé╔é═üCòKé╕RS_continue é≡Ä└ìsé╖éΘé▒é╞
  336. *************************************************************************/
  337.  
  338. void    RS_pause(void)
  339. {
  340.     int        port;
  341.  
  342.     if ( rsInit )
  343.     {
  344.         for ( port = 0; port < RS_PORTNUM; ++port )
  345.         {
  346.             if ( RsCtrl[port].use > 0 )
  347.             {
  348.                 if ( RsCtrl[port].overFlag == FALSE )
  349.                 {
  350.                     RsCtrl[port].overFlag = TRUE;
  351.                     RSB_CTRL(port,0x02);
  352.                 }
  353.                 RSB_DTR(port,1);        /*    DTRÉMìåé╠ò█Ä¥    */
  354.                 RSB_CLOSE(port);
  355.             }
  356.         }
  357. #ifndef    _RS_FMC
  358.         RSB_End();
  359. #endif
  360.     }
  361. }
  362.  
  363. /*************************************************************************
  364. *    ì─èJÅêù¥üiÄqâvâìâZâXî─é╤Åoé╡é╠é╜é▀üj
  365. *************************************************************************/
  366. void    RS_continue(void)
  367. {
  368.     int        port;
  369.  
  370.     if ( rsInit )
  371.     {
  372. #ifndef    _RS_FMC
  373.         RSB_Init();
  374. #endif
  375.         for ( port = 0; port < RS_PORTNUM; ++port )
  376.         {
  377.             if ( RsCtrl[port].use > 0 )
  378.             {
  379. #ifdef            _RS_FMC
  380.                 RSB_setpara(port, RsCtrl[port].rsb );
  381. #else
  382.                 RSB_Setpara(port, (char *)RsCtrl[port].rsb );
  383. #endif
  384.                 RSB_OPEN(port);
  385.             }
  386.         }
  387.     }
  388. }
  389.  
  390. int        RS_init(void)
  391. {
  392. #ifdef    _RS_FMC
  393.     return (0);
  394. #else
  395.     if ( rsInit )
  396.         return (NORMAL);
  397.     else
  398.     {    rsInit = TRU